package com.neu.carbon.mes.controller;

import java.util.Date;
import java.util.List;

import com.neu.carbon.mes.domain.*;
import com.neu.carbon.mes.service.*;
import com.neu.carbon.wms.domain.WmsMaterialInfo;
import com.neu.carbon.wms.service.IWmsMaterialInfoService;
import com.neu.common.enums.SerialNoPrefix;
import com.neu.smty.utils.CodeUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicResponseParameters;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.neu.common.annotation.Log;
import com.neu.common.core.controller.BaseController;
import com.neu.common.core.domain.AjaxResult;
import com.neu.common.enums.BusinessType;
import com.neu.common.utils.poi.ExcelUtil;
import com.neu.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 产品检验单Controller
 * 
 * @author neuedu
 * @date 2024-09-11
 */
@Api(tags = {"产品检验单"})
@RestController
@RequestMapping("/mesCheck/qaTraceReport")
public class MesProductCheckController extends BaseController
{
    @Autowired
    private IMesProductCheckService mesProductCheckService;
    @Resource
    private IWmsMaterialInfoService iWmsMaterialInfoService;
    @Resource
    private IMesProductJobService iMesProductJobService;
    @Resource
    private IMesProductPlanService iMesProductPlanService;
    @Resource
    private IMesCheckStandardService iMesCheckStandardService;
    @Resource
    private IMesFactoryModelService iMesFactoryModelService;
    @Resource
    private IMesProductScheduleService iMesProductScheduleService;

    /**
     * 查询产品检验单列表
     */
    @GetMapping("/list")
    @ApiOperation("查询产品检验单列表")
    @DynamicResponseParameters(properties = {
	        @DynamicParameter(name = "total", value = "总记录数"),
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "rows", value = "返回业务数据（数组类型）", dataTypeClass = MesProductCheck.class),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    public TableDataInfo list(MesProductCheck mesProductCheck)
    {
        startPage();
        List<MesProductCheck> list = mesProductCheckService.selectMesProductCheckList(mesProductCheck);

        list.forEach(model->{
            if(model.getCheckUser().equals("1")){
                model.setCheckUserName("管理员");
            }
            //质检标准
            MesCheckStandard mesCheckStandard = iMesCheckStandardService.selectMesCheckStandardById(model.getCheckStandardId());
            if (mesCheckStandard!=null){
                model.setCheckStandardName(mesCheckStandard.getCheckItemName());
            }
            //物料档案
            WmsMaterialInfo wmsMaterialInfo = iWmsMaterialInfoService.selectWmsMaterialInfoById(model.getMaterialId());
            if (wmsMaterialInfo!=null){
                model.setMaterialName(wmsMaterialInfo.getName());

            }
            //作业名称
            MesProductJob mesProductJob = iMesProductJobService.selectMesProductJobById(model.getProductJobId());
            if (mesProductJob!=null){
                model.setProductJobName(mesProductJob.getName());
            }
            //生产计划
            MesProductPlan mesProductPlan = iMesProductPlanService.selectMesProductPlanById(model.getProductPlanId());
            if (iMesProductPlanService!=null){
                model.setProductPlanName(mesProductPlan.getSerialNo());
            }
            //生产线
            MesFactoryModel mesFactoryModel = iMesFactoryModelService.selectMesFactoryModelById(model.getProductLineId());
            if (mesFactoryModel!=null){
                model.setProductLineName(mesFactoryModel.getProductLineName());
            }
            MesProductSchedule mesProductSchedule = iMesProductScheduleService.selectMesProductScheduleById(model.getProductScheduleId());
            if(mesProductSchedule!=null){
                model.setProductScheduleName(mesProductSchedule.getPlanName());
            }

        });

        return getDataTable(list);
    }

    /**
     * 导出产品检验单列表
     */
    @ApiOperation("导出产品检验单列表")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "msg", value = "文件名")
    })
    @PreAuthorize("@ss.hasPermi('mesCheck:qaTraceReport:export')")
    @Log(title = "产品检验单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MesProductCheck mesProductCheck)
    {
        List<MesProductCheck> list = mesProductCheckService.selectMesProductCheckList(mesProductCheck);
        ExcelUtil<MesProductCheck> util = new ExcelUtil<MesProductCheck>(MesProductCheck.class);
        return util.exportExcel(list, "qaTraceReport");
    }

    /**
     * 获取产品检验单详细信息
     */
    @ApiOperation("获取产品检验单详细信息")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "data", value = "返回业务数据", dataTypeClass = MesProductCheck.class),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        MesProductCheck model = mesProductCheckService.selectMesProductCheckById(id);

        if(model.getCheckUser().equals("1")){
            model.setCheckUserName("管理员");
        }
        //质检标准
        MesCheckStandard mesCheckStandard = iMesCheckStandardService.selectMesCheckStandardById(model.getCheckStandardId());
        if (mesCheckStandard!=null){
            model.setCheckStandardName(mesCheckStandard.getCheckItemName());
        }
        //物料档案
        WmsMaterialInfo wmsMaterialInfo = iWmsMaterialInfoService.selectWmsMaterialInfoById(model.getMaterialId());
        if (wmsMaterialInfo!=null){
            model.setMaterialName(wmsMaterialInfo.getName());

        }
        //作业名称
        MesProductJob mesProductJob = iMesProductJobService.selectMesProductJobById(model.getProductJobId());
        if (mesProductJob!=null){
            model.setProductJobName(mesProductJob.getName());
        }
        //生产计划
        MesProductPlan mesProductPlan = iMesProductPlanService.selectMesProductPlanById(model.getProductPlanId());
        if (iMesProductPlanService!=null){
            model.setProductPlanName(mesProductPlan.getSerialNo());
        }
        //生产线
        MesFactoryModel mesFactoryModel = iMesFactoryModelService.selectMesFactoryModelById(model.getProductLineId());
        if (mesFactoryModel!=null){
            model.setProductLineName(mesFactoryModel.getProductLineName());
        }
//    ·
        return AjaxResult.success(model);
    }

    /**
     * 新增产品检验单
     */
    @ApiOperation("新增产品检验单")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    @PreAuthorize("@ss.hasPermi('mesCheck:qaTraceReport:add')")
    @Log(title = "产品检验单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesProductCheck mesProductCheck)
    {

        return toAjax(mesProductCheckService.insertMesProductCheck(mesProductCheck));
    }

    /**
     * 修改产品检验单
     */
    @ApiOperation("修改产品检验单")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    @PreAuthorize("@ss.hasPermi('mesCheck:qaTraceReport:edit')")
    @Log(title = "产品检验单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesProductCheck mesProductCheck)
    {
        return toAjax(mesProductCheckService.updateMesProductCheck(mesProductCheck));
    }

    /**
     * 删除产品检验单
     */
    @ApiOperation("删除产品检验单")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    @PreAuthorize("@ss.hasPermi('mesCheck:qaTraceReport:remove')")
    @Log(title = "产品检验单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mesProductCheckService.deleteMesProductCheckByIds(ids));
    }
}
